//
//  TimeSelectView.m
//  SportSocial
//
//  Created by yull on 15/2/3.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "TimeSelectView.h"

@implementation TimeSelectView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        CGRect rect = CGRectMake(0, 40, self.width, 216);
        self.picker = [[UIDatePicker alloc] initWithFrame:rect];
        self.picker.datePickerMode = UIDatePickerModeDateAndTime;
        self.picker.locale = [NSLocale currentLocale];
        [self addSubview:self.picker];
        
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 40)];
        backView.backgroundColor = [UIColor whiteColor];
        [self addSubview:backView];
        
        rect = CGRectMake(10, 8, 40, 24);
        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelButton.frame = rect;
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton setTitleColor:RGB(201, 67, 72) forState:UIControlStateNormal];
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [backView addSubview:cancelButton];
        
        rect.origin.x = backView.width - 10 - rect.size.width;
        UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        confirmButton.frame = rect;
        [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [confirmButton setTitleColor:RGB(201, 67, 72) forState:UIControlStateNormal];
        confirmButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [backView addSubview:confirmButton];
        
        [cancelButton addTarget:self action:@selector(onClickCancelButton:) forControlEvents:UIControlEventTouchUpInside];
        [confirmButton addTarget:self action:@selector(onClickConfirmButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)onClickCancelButton:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(onCancel)]) {
        [self.delegate onCancel];
    }
}

- (void)onClickConfirmButton:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(onConfirmWithDate:)]) {
        [self.delegate onConfirmWithDate:self.picker.date];
    }
}
@end
